/**
 * <AiRmob-Framework; A simple augmented reality framework for android >
    Copyright (C) <2012>  <AiRmob Team>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package rendercomponents;

import android.opengl.GLES20;

public class ModelCube extends Model3D {
	
	
private float[] generateVertices(float a){	
	
	float cubeVertices[] = {
			
			 a, a, -a, 
			 -a, -a, a, 
			 1.0f, 1.0f,  
			 
			 a, -a, -a, 
			 -a, a, a, 
			 1.0f, 1.0f, 
			 
			 -a, -a, -a, 
			 a, a, a, 
			 1.0f, 1.0f,  
			 
			 a, a, -a,
			 -a, -a, a, 
			 1.0f, 1.0f, 
			 
			 -a, -a, -a, 
			 a, a, a, 
			 1.0f, 1.0f,
			 
			 -a, a, -a,
			 a, -a, a,
			 0.0f, 1.0f, 
			 
			 -a, -a, a, 
			 a, a, -a, 
			 0.0f, 0.0f, 
			 
			 -a, a, a,
			 a, -a, -a,
			 1.0f, 0.0f, 
			 
			 -a, a, -a,
			 a, -a, a,
			 1.0f, 1.0f,
			 
			 -a, -a, a,
			 a, a, -a,
			 0.0f, 0.0f, 
			 
			 -a, a, -a,
			 a, -a, a,
			 1.0f, 1.0f,  
			 
			 -a, -a, -a,
			 a, a, a,
			 0.0f, 1.0f,
			 
			 a, -a, a,
			 -a, a, -a,
			 0.0f, 0.0f, 
			 
			 a, a, a,
			 -a, -a, -a,
			 1.0f, 0.0f,  
			 
			 -a, -a, a,
			 a, a, -a,
			 0.0f, 1.0f,
			 
			 a, a, a,
			 -a, -a, -a,
			 1.0f, 0.0f,
			 
			 -a, a, a,
			 a, -a, -a,
			 1.0f, 1.0f,  
			 
			 -a, -a, a,
			 a, a, -a, 
			 0.0f, 1.0f,  
			 
			 a, -a, -a,
			 -a, a, a,
			 0.0f, 0.0f,  
			 
			 a, a, -a, 
			 -a, -a, a, 
			 1.0f, 0.0f, 
			 
			 a, -a, a, 
			 -a, a, -a, 
			 0.0f, 1.0f, 
			 
			 a, a, -a,
			 -a, -a, a,
			 1.0f, 0.0f,
			 
			 
			 a, a, a,
			 -a, -a, -a,
			 1.0f, 1.0f, 
			 
			 a, -a, a,
			 -a, a, -a,
			 0.0f, 1.0f,
			 
			 a, a, -a,
			 -a, -a, a, 
			 0.0f, 0.0f,
			 
			 -a, a, -a,
			 a, -a, a, 
			 1.0f, 0.0f, 
			 
			 
			 a, a, a,
			 -a, -a, -a,
			 0.0f, 1.0f,
			 
			 -a, a, -a,
			 a, -a, a,
			 1.0f, 0.0f,
			 
			 -a, a, a,
			 a, -a, -a,
			 1.0f, 1.0f, 
			 
			 a, a, a,
			 -a, -a, -a,
			 0.0f, 1.0f,
			 
			 a, -a, -a,
			 -a, a, a,
			 0.0f, 0.0f,
			 
			 a, -a, a,
			 -a, a, -a,
			 1.0f, 0.0f,
			 
			 -a, -a, a,
			 a, a, -a, 
			 1.0f, 1.0f, 
			 
			 a, -a, -a,
			 -a, a, a,
			 0.0f, 0.0f, 
			 
			 -a, -a, a,
			 a, a, -a, 
			 1.0f, 1.0f,
			 
			 -a, -a, -a,
			 a, a, a, 
			 0.0f, 1.0f,   
	 };
	return cubeVertices;
}
	
	
	
	short[] cubeIndices = {
			0, 1, 2,     3, 4, 5,     6, 7, 8, 
			9, 10, 11,   12, 13, 14,  15, 16, 17, 
			18, 19, 20,  21, 22, 23,  24, 25, 26,
			27, 28, 29,  30, 31, 32,  33, 34, 35
	};
	 
	 float ambient[] = {
		  0.4f, 0.4f, 0.4f, 1.0f
	 };
	 
	 float diffuse[] = {
			 0.8f, 0.8f, 0.8f, 1.0f
		 };
	 
	 float specular[] = {
			 1.0f, 1.0f, 1.0f, 1.0f
		 };
	 
	 float specular_exponent = 100.0f;
	 


	 /**
	  * Generates a cube with the given size. If size equals the size of the marker, the cube will fit exactly on it.
	  * Has a white texture and bright, white materialcolors.
	  * 
	  * @param r Used Renderer.
	  * @param size Size.
	  */
	 public ModelCube(Renderer r, float size){
		 super();
		 this.setbVertexBuffer(this.generateVertices(size));
		 this.setbIndexBuffer(cubeIndices);
		 this.normalsSet = true;
		 this.textureCoordsSet = true;
		 this.textureSet = true;
		 
		 this.setMaterialAmbient(ambient);
		 this.setMaterialDiffuse(diffuse);
		 this.setMaterialSpecular(specular);
		 this.setMaterialExponent(specular_exponent);
		 
		 this.setTexture(new Texture(r, "textures/white.jpg", ImageType.JPG));
		
		 if (r.settings.useVBO){
				int[] buffers = new int[3];
			    GLES20.glGenBuffers(3, buffers, 0);
				
				
				GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
		        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mesh.bVertexBuffer.capacity() * FLOAT_SIZE_BYTES, mesh.bVertexBuffer, GLES20.GL_STATIC_DRAW);
				
				GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
				GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.bIndexBuffer.capacity() * SHORT_SIZE_BYTES, this.bIndexBuffer, GLES20.GL_STATIC_DRAW);

				this.mesh.vBuffer = buffers[0];
				this.iBuffer = buffers[1];
				this.numIndices = this.bIndexBuffer.capacity();
	        
				System.out.println("Generating VBOs: " + this.mesh.vBuffer + ", " + this.iBuffer + ", " + this.numIndices);
			}
	 }

}